package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusSiteDetailActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryStandLinesRes;
import sz.szsmk.citizencard.mvp.contract.IBusSiteDetailContact;
import sz.szsmk.citizencard.mvp.presenter.BusSiteDetailPresenter;
import sz.szsmk.citizencard.ui.adapter.BusNearbyLineAdapter;
import sz.szsmk.citizencard.ui.adapter.BusSiteDetailAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/6 14:17
 * description:
 */
@Route("BusSiteDetailActivity")
public class BusSiteDetailActivity extends IBaseActivity<BusSiteDetailPresenter, BusSiteDetailActivityBinding>
        implements IBusSiteDetailContact.IBusSiteDetailView, View.OnClickListener {

    ImageView back;
    TextView settingTitle;
    ImageView shareIv;
    private RecyclerView busSiteRv;
    private QueryStandLinesRes standLinesRes;
    private List<QueryStandLinesRes.BusLineArraysBean> busLineArrays = new ArrayList<>();
    private BusNearbyLineAdapter lineAdapter;

    private List<QueryBusLineNearbyRes.BusLineArrayListBean> busLineArrayList = new ArrayList<>();
    private QueryBusLineNearbyRes.BusLineArrayListBean listBean;
    private List<QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean> mBean = new ArrayList<>();

    private List<QueryBusLineNearbyRes.BusLineArrayListBean> mBindList = new ArrayList<>();
    private BusSiteDetailAdapter busSiteDetailAdapter;
    private int stand_collection;
    private String stand_name;
    private String stand_guid;

    @Override
    protected int attachLayoutRes() {
        return R.layout.bus_site_detail_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);

    }

    @Override
    protected void initViews() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();


        back = findViewById(R.id.back);
        settingTitle = findViewById(R.id.setting_title);
        shareIv= findViewById(R.id.share_iv);

        shareIv.setVisibility(View.VISIBLE);
        stand_name = getIntent().getStringExtra("stand_name");
        stand_guid = getIntent().getStringExtra("stand_guid");
        showLoadingDialog();
        mPresenter.getSearchList(stand_name, stand_guid);
        busSiteRv = dataBinding.busSiteRv;
        settingTitle.setText("站点详情");
        back.setOnClickListener(this);
        shareIv.setOnClickListener(this);
//        busSiteRv.setLayoutManager(linearLayoutManager);
//        BusSiteDetailAdapter busSiteDetailAdapter = new BusSiteDetailAdapter(this);
//        busSiteRv.setAdapter(busSiteDetailAdapter);
        busSiteDetailAdapter = new BusSiteDetailAdapter(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        lineAdapter = new BusNearbyLineAdapter(this);
        dataBinding.busSiteRv.setLayoutManager(linearLayoutManager);
        dataBinding.busSiteRv.setAdapter(busSiteDetailAdapter);
    }

    @Override
    protected void updateViews() {
    }

    @Override
    public void getSearchListSucc(QueryStandLinesRes response) {
        dismissLoadingDialog();
        standLinesRes = response;
        busLineArrays = response.getBusLineArrays();
        stand_collection = response.getStand_collection();
        setStart();
        String stand_name = response.getStand_name();
        stand_guid = response.getStand_guid();
        dataBinding.busSiteName.setText(response.getStand_name());
        dataBinding.busSiteDes.setText("本站点有" + response.getSame_stand_name() + "个同名站台");
        dataBinding.busSiteAdress.setText(response.getCanton());

        busSiteDetailAdapter.setViewData(response.getBusLineArrays());
//        lineAdapter.setViewData(mBindList);
        busSiteDetailAdapter.notifyDataSetChanged();

    }

    private void setStart() {
        if (stand_collection == 0) {
            shareIv.setImageResource(R.drawable.icon_star_grey);
        } else {
            shareIv.setImageResource(R.drawable.icon_star_blue);
        }
    }

    @Override
    public void getSearchListFail(QueryStandLinesRes response) {
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT).show();
        dismissLoadingDialog();
    }

    @Override
    public void collectStandFail(BaseBean response) {
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void collectStandSucc(BaseBean response) {
        stand_collection = 1;
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT).show();
        setStart();
    }

    @Override
    public void cancelCollectStandFail(BaseBean response) {
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void cancelCollectStandSucc(BaseBean response) {
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT).show();
        stand_collection = 0;

        setStart();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.share_iv) {
            if (stand_collection == 0) {
                mPresenter.collectStand(stand_guid, stand_name);
//                    showLoadingDialog();
            } else {
                mPresenter.cancelCollectStand(stand_guid, stand_name);
//                    showLoadingDialog();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
